#include"link_list.h"
#include"func.h"
link_list* init_linklist()
{
   link_list*list=(link_list*)calloc(1,sizeof(link_list));
   if(list==NULL)
   {
    ERROR_CHECK(list,NULL,"create_list");
   }
   return list;
}
bool empty_linklist(link_list* list)
{
    return list->size==0;
}
void destory_linklist(link_list* list)
{
    if(list==NULL)
    return;
    else 
    {
        if(empty_linklist(list))
        free(list);
        else 
        {
            link_node* node=list->head;
            while(node!=NULL)
            {
                link_node*temp=node->next;
                free(node->value);
                free(node);
                node=temp;
            }
        }
    }
}
bool insert_linknode(link_list* list,int index,elemtype value)
{
    if(index<0||index>list->size)
    return false;
    link_node* newnode=(link_node*)calloc(1,sizeof(link_node));
    newnode->value = (elemtype)calloc(MAXSIZE, sizeof(char));
    ERROR_CHECK(newnode,NULL,"insert_node");
    strcpy(newnode->value, value);
    if(empty_linklist(list))
    {   
        list->head=newnode;
        list->tail=newnode;
        list->size++;
    }
    else if(index==0)//头插法
    {   
        newnode->next=list->head;
        list->head=newnode;
        list->size++;
    }
    else if(index==list->size)//尾插法
    {
        list->tail->next=newnode;
        list->tail=newnode;
        list->size++;
    }
    else 
    {
        link_node* node=list->head;
        int i=0;
        while(i<index-1)
        {
            node=node->next;
            i++;
        }
        newnode->next=node->next;
        node->next=newnode;
        list->size++;
    }
    return true;
}
bool delete_linknode(link_list*list,int index,elemtype val)
{
    if(empty_linklist(list)||index<0||index>list->size-1)
    return false;
    if(list->size==1)
    {
        free(list->head->value);
        free(list->head);
        list->head=NULL;
        list->tail=NULL;
        list->size--;
    }
    else if(index==0)
    {
        link_node*node=list->head;
        list->head=node->next;
        free(node->value);
        free(node);
        list->size--;
    }
    else if(index==list->size-1)
    {
        link_node*node=list->head;
        while(node->next!=list->tail)
        node=node->next;
        free(list->tail->value);
        free(list->tail);
        node->next=NULL;
        list->tail=node;
        list->size--;
    }
    else 
    {
        link_node*prev=list->head;
        int i=0;
        while(i++<index-2)
        prev=prev->next;
        link_node*node=prev->next;
        prev->next=node->next;
        free(node->value);
        free(node);
        list->size--;
    }
    return true;
}
bool getelem_linknode(link_list*list,int index,elemtype val)
{
    if(empty_linklist(list)||index<0||index>list->size-1)
    return false;
    int i=0;
    link_node *node=list->head;
    while(i<=index)
    {
        node=node->next;
        i++;
    }
    //*val=node->value;
    strcpy(val, node->value);
    return true;
}
bool location_linknode(link_list*list,int *index,elemtype val)
{
    if(empty_linklist(list))
    return false;
    int i=0;
    link_node*node=list->head;
    while(node!=NULL)
    {
        if(!strcmp(node->value,val))
        {
            *index=i;
            return true;
        }
        node=node->next;
        i++;
    }
    return false;
}
void printf_linklist(link_list *list)
{
    if(empty_linklist(list))
    return ;
    link_node *node=list->head;
    while(node!=NULL)
    {
        printf("%s ",node->value);
        node=node->next;
    }
    printf("\n");
}
void clear_linklist(link_list* list)
{
    if (empty_linklist(list))
        return;
    else
    {
        link_node* node = list->head;
        while (node != NULL)
        {
        link_node* temp = node->next;
        free(node->value);
        free(node);
        node = temp;
        }
        list->head = NULL;
        list->tail = NULL;
        list->size = 0;
        return;
    }
}